package com.yonyougov.devops.cruxcisupport.menu;

import com.yonyougov.devops.cruxcisupport.button.ButtonService;
import com.yonyougov.devops.cruxcisupport.component.UserStore;
import com.yonyougov.devops.cruxcisupport.menu.dto.MenuAddRequestDto;
import com.yonyougov.devops.cruxcisupport.menu.dto.MenuAllNoLinkRequestDto;
import com.yonyougov.devops.cruxcisupport.menu.dto.MenuDeleteRequestDto;
import com.yonyougov.devops.cruxcisupport.menu.dto.MenuSinglePageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.menu.vo.AllMenuLinkRespVo;
import com.yonyougov.devops.cruxcisupport.menu.vo.NoLinkMenuByRoleIdVo;
import com.yonyougov.devops.cruxcisupport.menu.vo.NoLinkMenuPageByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.RoleMenuService;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;


/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "菜单模块", tags = "菜单模块")
@RestController
@RequestMapping("/api/v2/menu")
public class MenuController extends CrudController<MenuEntity, BigInteger> {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private UserStore userStore;

    @Autowired
    private ButtonService buttonService;

    @Override
    public BaseService<MenuEntity, BigInteger> getService() {
        return menuService;
    }

    //<!--用户自定义代码开始-->

    @ApiModelProperty(value = "菜单管理模块调用", notes = "新增一个菜单")
    @PostMapping("/m/add")
    @SneakyThrows
    public BaseResponseDto add(@RequestBody @Valid MenuAddRequestDto menuAddRequestDto) {
        MenuEntity buttonEntity = new MenuEntity();
        BeanUtils.copyProperties(menuAddRequestDto, buttonEntity);

        menuService.add(buttonEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "菜单管理模块调用", notes = "删除一个菜单")
    @PostMapping("/m/delete/id")
    @SneakyThrows
    public BaseResponseDto delete(@RequestBody @Valid MenuDeleteRequestDto request) {

        menuService.deleteOneMenuByMenuId(request.getId());

        return BaseResponseDto.getSuccessInstance();
    }


    @ApiOperation(value = "菜单管理模块首页渲染数据调用", notes = "分页查询全部")
    @PostMapping(value = "/m/single/page/like")
    public BaseResponseDto findAllSinglePage(@RequestBody @Valid MenuSinglePageLikeRequestDto request) {
        MenuEntity menuEntity = new MenuEntity();
        BeanUtils.copyProperties(request, menuEntity);

        Pagination<MenuEntity> menuEntityPagination = menuService.likeAllBy(menuEntity);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", menuEntityPagination.getTotalCount());
        result.put("data", menuEntityPagination.getDatas());
        return result;
    }

    @ApiOperation(value = "暂未调用", notes = "单表查询全部")
    @PostMapping(value = "/m/single/like")
    public BaseResponseDto findAllSingle(@RequestBody @Valid MenuSinglePageLikeRequestDto request) {
        MenuEntity menuEntity = new MenuEntity();
        BeanUtils.copyProperties(request, menuEntity);
        menuEntity.setPageAble(false);

        Pagination<MenuEntity> menuEntityPagination = menuService.likeAllBy(menuEntity);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", menuEntityPagination.getDatas());
        return result;
    }


    @ApiModelProperty(value = "角色管理模块-角色关联菜单调用", notes = "根据角色id查询未被该角色关联的菜单")
    @PostMapping(value = "/m/all/nolink/roleid")
    public BaseResponseDto findAllMenuNoLinkByRoleId(@RequestBody @Valid MenuAllNoLinkRequestDto request) {
        NoLinkMenuPageByRoleIdDto noLinkMenuPageByRoleIdDto = new NoLinkMenuPageByRoleIdDto();
        BeanUtils.copyProperties(request, noLinkMenuPageByRoleIdDto);

        Pagination<NoLinkMenuByRoleIdVo> pagination = menuService.findNoLinkMenuPageByRoleId(noLinkMenuPageByRoleIdDto);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("totalCount", pagination.getTotalCount());
        baseResponseDto.put("data", pagination.getDatas());
        return baseResponseDto;
    }

    @ApiModelProperty(value = "首页渲染菜单使用", notes = "当前用户已经关联的菜单")
    @PostMapping(value = "/m/all/linked/userid")
    public BaseResponseDto findAllMenuLinkByUserId() {
        List<AllMenuLinkRespVo> dates = menuService.findLinkMenuByUserId(userStore.getCurrentUser().getId());

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", dates);
        return baseResponseDto;
    }

    @ApiModelProperty(value = "暂未调用", notes = "顶级菜单")
    @PostMapping(value = "/m/all/root")
    public BaseResponseDto findAllRootMenu() {

        List<MenuEntity> datas = menuService.findAllRootMenu();

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", datas);
        return baseResponseDto;
    }
    //<!--用户自定义代码结束-->
}
